﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Web;
using System.Web.Configuration;
using System.Web.Hosting;
using Critex.Models;

namespace Critex.Helpers
{
    public static class SetupHelper
    {

        public static User CreateEditingUser(string username, string password)
        {
            var container = ConnectionHelper.GetContainer();
            if (!container.Users.Any(u => u.Username == username))
            {
                var user = User.CreateUser(0, username, password);
                container.Users.AddObject(user);

                container.SaveChanges();

                return user;
            }

            return container.Users.First(u => u.Username == username);
        }

        public static void CreateHtmlArea(string title, Section section)
        {
            var container = ConnectionHelper.GetContainer();
            if (!container.ContentAreas.Any(u => u.Title == title && u.Section.Id == section.Id))
            {
                var area = HtmlArea.CreateHtmlArea(0, title);
                area.Section = section;
                section.ContentAreas.Add(area);
                container.ContentAreas.AddObject(area);

                container.SaveChanges();
            }
        }

        public static void CreateImageArea(string title, Section section, ImageCollectionArea imageCollection = null)
        {
            var container = ConnectionHelper.GetContainer();
            if (!container.ContentAreas.Any(u => u.Title == title && u.Section.Id == section.Id))
            {
                var area = ImageArea.CreateImageArea(0, title);
                area.Section = section;
                section.ContentAreas.Add(area);
                area.ImageCollectionArea = imageCollection;
                container.ContentAreas.AddObject(area);

                container.SaveChanges();
            }
        }

        public static void CreateImageCollectionArea(string title, Section section)
        {
            var container = ConnectionHelper.GetContainer();
            if (!container.ContentAreas.Any(u => u.Title == title && u.Section.Id == section.Id))
            {
                var area = ImageCollectionArea.CreateImageCollectionArea(0, title);
                area.Section = section;
                section.ContentAreas.Add(area);
                container.ContentAreas.AddObject(area);

                container.SaveChanges();
            }
        }

        public static void CreateHtmlCollectionArea(string title, Section section)
        {
            var container = ConnectionHelper.GetContainer();
            if (!container.ContentAreas.Any(u => u.Title == title && u.Section.Id == section.Id))
            {
                var area = HtmlCollectionArea.CreateHtmlCollectionArea(0, title);
                area.Section = section;
                section.ContentAreas.Add(area);
                container.ContentAreas.AddObject(area);

                container.SaveChanges();
            }
        }

        public static Section CreateSection(string title, params Section[] parentSections)
        {
            var container = ConnectionHelper.GetContainer();
            if (!container.Sections.Any(u => u.Title == title))
            {
                var section = Section.CreateSection(0, title);
                foreach (var parent in parentSections)
                {
                    if (parent != null)
                    {
                        section.ParentSections.Add(parent);
                        parent.SubSections.Add(section);
                    }
                }
                container.Sections.AddObject(section);

                container.SaveChanges();

                return section;
            }

            return container.Sections.First(u => u.Title == title);
        }

        public static void Setup(string styleLocation)
        {

            var container = ConnectionHelper.GetContainer();
            if(!container.Settings.Any())
            {
                var settings = Settings.CreateSettings(0, styleLocation);
                container.Settings.AddObject(settings);
            }
            else
            {
                var settings = container.Settings.First();
                settings.StyleLocation = styleLocation;
            }
            container.SaveChanges();
        }

        public static void SetupSample()
        {
            SetupHelper.Setup("/Content/Dashboard.css");

            SetupHelper.CreateEditingUser("Test", "1234");

            var mainPage = SetupHelper.CreateSection("Test main page");
            var galleryPage = SetupHelper.CreateSection("Test gallery page");

            var middlePane = SetupHelper.CreateSection("Middle pane", mainPage, galleryPage);
            SetupHelper.CreateHtmlArea("Content", middlePane);

            var menu = SetupHelper.CreateSection("Menu", mainPage, galleryPage);
            SetupHelper.CreateHtmlCollectionArea("Items", menu);

            var gallery = SetupHelper.CreateSection("Gallery", galleryPage);
            SetupHelper.CreateImageCollectionArea("Gallery content", gallery);
            SetupHelper.CreateHtmlArea("Gallery description", gallery);
        }

        public static void CreateAddressArea(string title, Section section)
        {
            var container = ConnectionHelper.GetContainer();
            if (!container.ContentAreas.Any(u => u.Title == title && u.Section.Id == section.Id))
            {
                var area = AddressArea.CreateAddressArea(0, title);
                area.Section = section;
                section.ContentAreas.Add(area);
                container.ContentAreas.AddObject(area);

                container.SaveChanges();
            }
        }
    }
}